package com.fjwt.gz.service.activity.platform.acte024;

import com.alibaba.fastjson2.JSONObject;
import com.fjwt.gz.core.constants.Constants;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.db.entity.*;
import com.fjwt.gz.db.vo.ActivityAwardVO;
import com.fjwt.gz.db.vo.AwardVirtualVO;
import com.fjwt.gz.db.vo.ExportAwardVO;
import com.fjwt.gz.service.activity.AbstractExportReport;
import com.fjwt.gz.service.impl.*;
import com.fjwt.gz.util.quartz.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * ------ 邀请助力活动导出报表
 */
@Slf4j
@Service
public class ACTE024ExportReport extends AbstractExportReport<ExportAwardVO> {

    @Autowired
    private ActivityAwardService activityAwardService;
    @Autowired
    private ScoreRecordTotalService scoreRecordTotalService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private AwardCouponService awardCouponService;
    @Autowired
    private AwardGoodsService awardGoodsService;
    @Autowired
    private AwardVirtualService awardVirtualService;


    private static final Map<String, String> headerAliasMap = new HashMap<>();
    static {
        headerAliasMap.put("fansId", "用户唯一ID");
        headerAliasMap.put("realName", "参与客户姓名");
        headerAliasMap.put("phone", "参与客户手机号");
        headerAliasMap.put("isAuth", "参与客户是否实名");
        headerAliasMap.put("couponName", "领用卡券名称");
        headerAliasMap.put("couponPrice", "卡券金额");
        headerAliasMap.put("couponThirdId", "微信唯一卡券编号");
        headerAliasMap.put("createdAt", "参与时间");
        headerAliasMap.put("writeTime", "核销时间");
        headerAliasMap.put("status", "状态");
        headerAliasMap.put("mchNo", "发放商户号");
        headerAliasMap.put("mchName", "发放商户名称");
        headerAliasMap.put("goodsName", "奖品名称");
        headerAliasMap.put("score", "积分值");
        headerAliasMap.put("name", "积分名称");
    }


    @Override
    public String exportActivityExcel(SysExportTaskEntity sysExportTaskEntity) {
        ActivityEntity activityEntity = getRequestParam(sysExportTaskEntity.getRequestParam(), ActivityEntity.class);
        ActivityEntity activityEntitys = activityService.getBaseMapper().selectById(activityEntity.getActivityId());
        List<ActivityAwardEntity> activityAwardList = activityAwardService.lambdaQuery().eq(ActivityAwardEntity::getActivityId, activityEntity.getActivityId()).list();

        // 分别收集不同类型的awardId
        List<Long> couponIds = new ArrayList<>();
        List<Long> goodsIds = new ArrayList<>();
        List<Long> virtualIds = new ArrayList<>();

        activityAwardList.forEach(award -> {
            switch (award.getAwardType()) {
                case 0: // 优惠券类型
                    couponIds.add(award.getAwardId());
                    break;
                case 1: // 商品类型
                    goodsIds.add(award.getAwardId());
                    break;
                case 2: // 虚拟奖品类型
                    virtualIds.add(award.getAwardId());
                    break;
            }
        });
        List<ExportAwardVO> voList = new ArrayList<>();
        // 批量查询各种奖品信息
        if (!couponIds.isEmpty()) {
            List<AwardCouponEntity> awardCouponList = awardCouponService.lambdaQuery()
                    .in(AwardCouponEntity::getCouponId, couponIds).list();
            for (AwardCouponEntity entity : awardCouponList) {
                ExportAwardVO vo = new ExportAwardVO();
                BeanUtils.copyProperties(entity, vo);  // 复制同名属性
                voList.add(vo);
            }
        }

        if (!goodsIds.isEmpty()) {
            List<AwardGoodsEntity> awardGoodsEntityList = awardGoodsService.lambdaQuery()
                    .in(AwardGoodsEntity::getGoodsId, goodsIds).list();
            for (AwardGoodsEntity entity : awardGoodsEntityList) {
                ExportAwardVO vo = new ExportAwardVO();
                BeanUtils.copyProperties(entity, vo);  // 复制同名属性
                voList.add(vo);
            }
        }

        if (!virtualIds.isEmpty()) {
            List<AwardVirtualVO> awardVirtualEntityList = awardVirtualService.getListByVirtualIdList(virtualIds);
            for (AwardVirtualVO entity : awardVirtualEntityList) {
                ExportAwardVO vo = new ExportAwardVO();
                BeanUtils.copyProperties(entity, vo);  // 复制同名属性
                voList.add(vo);
            }
        }

        List<ScoreRecordTotalEntity> scoreRecordTotalList = scoreRecordTotalService.lambdaQuery()
                .eq(ScoreRecordTotalEntity::getActivityId, activityEntity.getActivityId())
                .list();
        if (!scoreRecordTotalList.isEmpty()){
            for (ScoreRecordTotalEntity entity : scoreRecordTotalList) {
                ExportAwardVO vo = new ExportAwardVO();
                BeanUtils.copyProperties(entity, vo);  // 复制同名属性
                voList.add(vo);
            }
        }

        return createExcelAndUploadOssUrl(voList, headerAliasMap, sysExportTaskEntity);
    }
}
